#include "OMInterfaceDX9.h"
#include "../Texture/Texture2DDX9.h"

COMInterfaceDX9::COMInterfaceDX9( )
{
    m_uRenderTargetsCount = 0;
    m_bUseDepthStencil    = false;
    m_ppRenderTarget      = NULL;
    m_pOrigRT             = NULL;
    m_ViewPort.MinZ       = 0.0f;
    m_ViewPort.MaxZ       = 1.0f;
    m_ViewPort.X          = 0;
    m_ViewPort.Y          = 0;
    m_ViewPort.Height     = 0;
    m_ViewPort.Width      = 0;
}

COMInterfaceDX9::~COMInterfaceDX9( )
{
    for (mUInt i = 0; i < m_uRenderTargetsCount; i++)
    {
        SAFE_RELEASE(m_ppRenderTarget[i]);
    }
    SAFE_RELEASE(m_pOrigRT);
    SAFE_RELEASE(m_pOrigDS);
    delete [] m_ppRenderTarget;
    if (m_bUseDepthStencil)
        SAFE_RELEASE(m_pDS);
}

mVoid COMInterfaceDX9::CreateRenderTarget( CTexture2D *a_pTexture[], mUInt a_uRenderTargetsCount )
{
    if (m_uRenderTargetsCount > 0)
        return;
    m_uRenderTargetsCount = a_uRenderTargetsCount;
    m_ppRenderTarget      = new IDirect3DSurface9*[m_uRenderTargetsCount];
    m_ViewPort.Width      = a_pTexture[0]->Width();
    m_ViewPort.Height     = a_pTexture[0]->Height();
    CTexture2DDX9 *pTexDX9;
    for (mUInt i = 0; i < m_uRenderTargetsCount; i++)
    {        
        pTexDX9 = static_cast< CTexture2DDX9* >(a_pTexture[i]);
        pTexDX9->Texture()->GetSurfaceLevel(0, &m_ppRenderTarget[i]);
    }
}

mVoid COMInterfaceDX9::CreateDepthStencil( CTexture2D *a_pTexture )
{
    CTexture2DDX9 *pTexDX9 = static_cast< CTexture2DDX9* >(a_pTexture);
    if (m_ViewPort.Width == 0)
    {
        m_ViewPort.Width      = a_pTexture->Width();
        m_ViewPort.Height     = a_pTexture->Height();
    }
    else 
    {
        if (m_ViewPort.Width != a_pTexture->Width() || m_ViewPort.Height != a_pTexture->Height())
            return;//some shit must happens, mmm
        pTexDX9->Texture()->GetSurfaceLevel(0, &m_pDS);
    }
    m_bUseDepthStencil = true;
}

mVoid COMInterfaceDX9::SaveCurrentRT( )
{
    g_D3DDevice.pD3D9Device->GetViewport(&m_PrevViewPort);
    g_D3DDevice.pD3D9Device->GetRenderTarget(0, &m_pOrigRT);
    g_D3DDevice.pD3D9Device->GetDepthStencilSurface(&m_pOrigDS);
}

mVoid COMInterfaceDX9::RestoreRT( )
{
    g_D3DDevice.pD3D9Device->SetDepthStencilSurface(m_pOrigDS);
    g_D3DDevice.pD3D9Device->SetViewport(&m_PrevViewPort);
    if (m_uRenderTargetsCount > 0)
    {
        g_D3DDevice.pD3D9Device->SetRenderTarget(0, m_pOrigRT);
        for (mUInt i = 1; i < m_uRenderTargetsCount; i++)
        {
            g_D3DDevice.pD3D9Device->SetRenderTarget(i, NULL);
        }
    }    
}

mVoid COMInterfaceDX9::SetRenderTargets( )
{
    g_D3DDevice.pD3D9Device->SetViewport(&m_ViewPort);
    if (m_uRenderTargetsCount > 0)
    {
        for (mUInt i = 0; i < m_uRenderTargetsCount; i++)
        {
            g_D3DDevice.pD3D9Device->SetRenderTarget(i, m_ppRenderTarget[i]);
        }
    }
    
    if (m_bUseDepthStencil)
        g_D3DDevice.pD3D9Device->SetDepthStencilSurface(m_pDS);
    else
        g_D3DDevice.pD3D9Device->SetDepthStencilSurface(NULL);
}